package com.example.demo1;

import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class User {
    private String name;
    private String pwd;
    private String phone;
    private String email;
    //存储文件名
    private  static  final  String SHARED_NAME="userInfo";


    //文件读取
    FileInputStream fileInputStream=null;
    FileOutputStream fileOutputStream=null;
    //存储文件在data/data/com.example.demo1/files

    public  void saveUserByFile(Context context) {
        String myuserdata;
        myuserdata=name+","+pwd+","+email+","+phone;
        try {
            fileOutputStream =context.openFileOutput(SHARED_NAME,Context.MODE_PRIVATE);
            fileOutputStream.write(myuserdata.getBytes());
            fileOutputStream.flush();
        }catch (Exception e){
        }finally {
            if(fileOutputStream!=null){
                try{
                    fileOutputStream.close();
                }catch (Exception e){
                }
            }
        }

    }

    public void getUserByFile(Context context){
        //文件读取
        try {
            fileInputStream=context.openFileInput(SHARED_NAME);
            byte[] buffer=new byte[fileInputStream.available()];
            fileInputStream.read(buffer);
            String userData=new String(buffer);
            String info[]=userData.split(",");
            System.out.println(info[0]);
            name=info[0];
            phone=info[3];
            pwd=info[1];
            email=info[2];
        }catch (Exception e){
        }finally {
        }
    }


    //SharedPreferences方法
    //存储文件在data/data/com.example.demo1/shared_prefs
    SharedPreferences sharedPreferences;

    public  void saveUserBySharedPreferences(Context context) {

        sharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("name",name);
        editor.putString("pwd",pwd);
        editor.putString("email",email);
        editor.putString("phone",phone);
        editor.commit();
    }

    public void getUserBySharedPreferences(Context context){
        sharedPreferences = context.getSharedPreferences(SHARED_NAME, Context.MODE_PRIVATE);
        name=sharedPreferences.getString("name","");
        phone=sharedPreferences.getString("phone","");
        pwd=sharedPreferences.getString("pwd","");
        email=sharedPreferences.getString("email","");
    }


    //SQLite存储
    SQLiteDatabase db;

    public  int  insertBySQLite(Context context){
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db=helper.getWritableDatabase();
        values.put(DBUtil.STU_NAME,name);
        values.put(DBUtil.STU_PHONE,phone);
        values.put(DBUtil.STU_PWD,pwd);
        values.put(DBUtil.STU_EMAIL,email);
        long number=db.insert(DBUtil.TABLE_NAME,null,values);
        return (int)number;
    }

    public int delBySQLite(Context context){
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getWritableDatabase();
        int number = db.delete(DBUtil.TABLE_NAME, DBUtil.STU_NAME+"=?", new String[]{name+""});
        return  number;
    }

    public int updateBySQLite(Context context){
        DBHelper helper=new DBHelper(context);
        db = helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(DBUtil.STU_PHONE, phone);
        values.put(DBUtil.STU_PWD, pwd);
        values.put(DBUtil.STU_EMAIL, email);
        int number = db.update(DBUtil.TABLE_NAME, values, DBUtil.STU_NAME+"=?",new String[]{name+""});
        return number;
    }

    public int selectByNameBySQLite(Context context){
        DBHelper helper=new DBHelper(context);
        ContentValues values=new ContentValues();
        db = helper.getReadableDatabase();
        Cursor cursor=null;
        cursor = db.query(DBUtil.TABLE_NAME, null, DBUtil.STU_NAME+"=?", new String[]{name+""},null, null, null);
        if (cursor.getCount() != 0){
            while (cursor.moveToNext()){
                name = cursor.getString(cursor.getColumnIndex(DBUtil.STU_NAME));
                phone = cursor.getString(cursor.getColumnIndex(DBUtil.STU_PHONE));
                pwd = cursor.getString(cursor.getColumnIndex(DBUtil.STU_PWD));
                email = cursor.getString(cursor.getColumnIndex(DBUtil.STU_EMAIL));
                //Toast.makeText(this, name1+","+phone1+","+pwd1+","+email1, Toast.LENGTH_SHORT).show();
            }
        }
        return  0;
    }










    public User() {
    }

    public User(String name, String pwd, String phone, String email) {
        this.name = name;
        this.pwd = pwd;
        this.phone = phone;
        this.email = email;
    }

    public User(String name, String pwd, String phone) {
        this.name = name;
        this.pwd = pwd;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "" + name + ",恭喜你注册成功：" + '\''+
                ", 手机为：'" + phone + '\'' +
                ", 邮箱为：'" + email + '\'' ;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
